package robocup.component.actions;

import robocup.component.SConf;
import robocup.component.geometry.Vektor;
import robocup.component.worldmodel.ShortTermMemory.VEKTORS;

/**
 * A TackleAction action is used to execute a tackle command. The effect of that
 * action is uncertain, and the costs are high. If the ball is more distant, the
 * chance of successful tacklings reduces fast. After tackling (in both cases,
 * successful or not) the agent can't move for an amount of cycles as defined in
 * SConf.TACKLE_CYCLES.<br>
 * 
 * Note that tackling can only be performed into two directions - the body
 * direction or the opposite one.
 */
public class TackleAction
    extends Action {

//  /**
//   * The power of the tackle command. This has the same meaning as the power of
//   * the KickAction. The only difference is, that it can be set to values of
//   * [-100,100] to enable backward tacklings (because of the missing direction
//   * parameter).
//   */
//  private double _powers;

  /**
   * Since v12. of SoccerServer: tackle can only control
   * direction, which indirectly has an impact on the power.
   */
  private double _direction;
  
  /**
   * Constructor - creates a tackle-action
   * 
   * @param direction - [-180, 180] the dierction relative
   *  to the player. The power depends on the direction.
   */
  public TackleAction(
      final double dir) {

    this._direction = dir;
    this._type = TYPE.TACKLE;
  }

  /**
   * New Calculation, used since Server Version 12.0
   * @See news-file of the soccer-server
   *
   * @return Returns the power.
   */
  public double getMaxPower() {
	  double effPower = (
		  SConf.getInstance().MAX_BACK_TACKLE_POWER + 
		  (SConf.getInstance().MAX_TACKLE_POWER - 
				  SConf.getInstance().MAX_BACK_TACKLE_POWER) * 
				  (1. - Math.abs(Math.toRadians(Vektor.normalize(_direction)))/Math.PI)) * 
				  SConf.getInstance().TACKLE_POWER_RATE;
    return effPower;
  }

  
  public double getPower(double player_to_ball){
    return (getMaxPower() * (1.0 - 0.5*(Math.toRadians(player_to_ball)/Math.PI) ));    
  }
  /**
   * Sets the power.
   * 
   * @param power
   *          the tackle power: [-100,100]
   */
  public void setDirection(double dir) {

    this._direction = dir;
  }

  /**
   * Returns the actions string representation:
   * 
   * @return (tackle &lt;power&gt;)<br>
   *         <br>
   */
  @Override
  public String toString() {

    final StringBuffer buf = new StringBuffer();
    buf.append("(tackle ");
    buf.append(Vektor.normalize(this._direction));
    buf.append(")");
    return buf.toString();
  }

  /**
   * Getter for the direction
   * @return
   */
  public double getDirection() {
	// TODO Auto-generated method stub
	return this._direction;
  }

}
